package com.eastedu.util;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * The type Url parser.
 *
 * @author zhangzhishuai
 */
public class UrlParser {
    private static final Pattern URL_PATTERN = Pattern.compile(".*/(.*)$");

    /**
     * Extract text from url string.
     *
     * @param url the url
     * @return the string
     */
    public static Optional<String> extractFileNameFromUrl(String url) {
        Matcher matcher = URL_PATTERN.matcher(url);
        if (matcher.find()) {
            String fileName = matcher.group(1);
            fileName = fileName.substring(0, fileName.lastIndexOf('.'));
            return Optional.of(fileName);
        }
        return Optional.empty();
    }
}